(function() {
    'use strict';

    angular
        .module('byibserverApp')
        .controller('ProductDialogController', ProductDialogController)

    ProductDialogController.$inject = ['$timeout', '$scope', '$stateParams', '$uibModalInstance', 'entity', 'Product', 'Isurer', 'TypesOfInsurance', 'WordKey','FileUploader'];

    function ProductDialogController ($timeout, $scope, $stateParams, $uibModalInstance, entity, Product, Isurer, TypesOfInsurance, WordKey,FileUploader) {
        var vm = this;

        vm.product = entity;
        vm.clear = clear;
        vm.save = save;
      //  vm.isurers = Isurer.query();
        getIsurers();

        function getIsurers() {
            Isurer.query({
                page: 0,
                size: 1000,
            }, function (data,headers) {
                vm.isurers = data;
            }, function (error) {

            });
        }

        vm.typesofinsurances = TypesOfInsurance.query();
       // vm.wordkeys = WordKey.query();
        getWorkKeys();

        function getWorkKeys() {
            WordKey.query({
                page: 0,
                size: 1000,
            }, function (data,headers) {
                vm.wordkeys = data;
            }, function (error) {

            });
        }

        var uploader=$scope.uploader = new FileUploader({
            url: 'http://www.biyibibaoxian.com/java/api/up-file'
        });
        // 回调
        uploader.onSuccessItem = function(fileItem, response, status, headers) {
            console.info('onSuccessItem', fileItem, response, status, headers);
            //  imageLink
            vm.product.purchaseLink=response.image_link;
        };
        uploader.onErrorItem = function(fileItem, response, status, headers) {
            console.info('onErrorItem', fileItem, response, status, headers);
        };

        console.log(uploader)

        // 过滤器
        uploader.filters.push({
            name: 'customFilter',
            fn: function(item /*{File|FileLikeObject}*/, options) {
                return this.queue.length < 10;
            }
        });




        console.log("\n\n\n vm.uploader------->",uploader,"\n\n\n")

        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.product.id !== null) {
                Product.update(vm.product, onSaveSuccess, onSaveError);
            } else {
                Product.save(vm.product, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('byibserverApp:productUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }


    }



})();
